package com.yinp.proapp.module.im.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.recyclerview.widget.LinearLayoutManager;

import com.yinp.proapp.activity.bean.MyUserInfo;
import com.yinp.proapp.adapter.CommonAdapter;
import com.yinp.proapp.adapter.SingleViewHolder;
import com.yinp.proapp.base.fragment.AppBaseFragment;
import com.yinp.proapp.databinding.FragmentImTwoBinding;
import com.yinp.proapp.databinding.ItemFriendsListBinding;
import com.yinp.proapp.module.im.activity.SingleCharActivity;
import com.yinp.proapp.utils.GlideUtils;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.QueryListener;

/**
 * @author :yinpeng
 * date      :2021/11/23
 * package   :com.yinp.proapp.module.im.fragment
 * describe  :群组，联系人界面
 */
public class ImFragmentTwo extends AppBaseFragment<FragmentImTwoBinding> {
    private CommonAdapter<MyUserInfo> friedAdapter;
    private List<MyUserInfo> friedList = new ArrayList<>();

    @Override
    protected void initViews(View view) {
        initRecycler();
        getList();
    }

    private void initRecycler() {
        friedAdapter = new CommonAdapter<MyUserInfo>(mContext,friedList) {
            @Override
            protected SingleViewHolder setComViewHolder(View view, int viewType, ViewGroup parent) {
                return new SingleViewHolder(ItemFriendsListBinding.inflate(mInflater, parent, false));
            }

            @Override
            public void onBindItem(SingleViewHolder holder, int position, MyUserInfo item) {
                ItemFriendsListBinding binding = (ItemFriendsListBinding) holder.getBinding();
                GlideUtils.intoRadius(getContext(), item.getHeadUrl(), binding.ivHead, 100);
                binding.tvName.setText(TextUtils.isEmpty(item.getNickName()) ? item.getUsername() : item.getNickName());
            }
        };
        friedAdapter.setOnItemClickListener((position, view) -> {
            Bundle bundle = new Bundle();
            MyUserInfo bean = friedList.get(position);
            bundle.putString("userId", bean.getObjectId());
            bundle.putString("userName", TextUtils.isEmpty(bean.getNickName()) ? bean.getUsername() : bean.getNickName());
            bundle.putString("headUrl", bean.getHeadUrl());

            //查找Person表里面id为-----的数据
            BmobQuery<MyUserInfo> bmobQuery = new BmobQuery<MyUserInfo>();
            bmobQuery.getObject(bean.getObjectId(), new QueryListener<MyUserInfo>() {
                @Override
                public void done(MyUserInfo object, BmobException e) {
                    if (e == null) {
                        goToActivity(SingleCharActivity.class, bundle);
                    } else {
                        showToast("当前账号已注销");
                        getList();
                    }
                }
            });

        });
        bd.rvFriendList.setLayoutManager(new LinearLayoutManager(getContext()));
        bd.rvFriendList.setHasFixedSize(true);
        bd.rvFriendList.setAdapter(friedAdapter);
    }

    private void getList() {
        BmobQuery<MyUserInfo> query = new BmobQuery<>();
        query.findObjects(new FindListener<MyUserInfo>() {
            @Override
            public void done(List<MyUserInfo> list, BmobException e) {
                if (e == null) {
                    Log.d(TAG, "查询user成功" + list);
                    friedList.clear();
                    friedList.addAll(list);
                    friedAdapter.notifyDataSetChanged();
                } else {

                }
            }
        });
    }
}
